package com.digitalchina.lxnsy.toolkit;

import android.app.Activity;
import android.app.ProgressDialog;
import android.os.Handler;
import android.os.Message;
import android.util.Log;

import androidx.annotation.Nullable;

import com.alibaba.fastjson.JSON;
import com.apkfuns.logutils.LogUtils;
import com.digitalchina.lxnsy.constant.Constant;
import com.digitalchina.lxnsy.entity.HeaderVO;
import com.digitalchina.lxnsy.entity.ResultVO;
import com.digitalchina.lxnsy.xml.RongCloudXML;
import com.digitalchina.lxnsy.xml.UserXML;
import com.lzy.okhttputils.OkHttpUtils;
import com.lzy.okhttputils.callback.StringCallback;
import com.lzy.okhttputils.model.HttpParams;
import com.lzy.okhttputils.request.BaseRequest;
import com.ta.utdid2.android.utils.StringUtils;

import org.json.JSONException;
import org.json.JSONObject;

import java.net.SocketTimeoutException;
import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeoutException;

import okhttp3.Call;
import okhttp3.MediaType;
import okhttp3.Request;
import okhttp3.Response;

import static com.lzy.okhttputils.request.PostRequest.MEDIA_TYPE_JSON;

/**
 * ================================================
 * 项目名称：杨凌农业大数据(数据采集)
 * 类描述：
 * 包名：com.digitalchina.bigdata.toolkit
 * 创建人：郑皓
 * 创建时间：2016/4/12 16:22
 * 修改备注：
 * ================================================
 */


public class OkHttpUtil {


    /**
     * put访问服务器
     *
     * @param activity
     * @param httpUrl
     * @param dialogMsg 加载框提示语 如果为空 则不显示加载框
     * @param params
     * @param handler
     * @param success
     * @param failed
     */
    public static void getNull(final Activity activity, String httpUrl, final String dialogMsg, HttpParams params, final Handler handler, final int success,
                               final int failed) {

        final Message msg = new Message();


        LogUtils.e(params);

        if (!Utils.isInternetAvailable(activity)) {
            msg.what = failed;
            msg.obj = "网络链接不可用，请检查网络";
            handler.sendMessage(msg);
        } else {
            OkHttpUtils.get(httpUrl)
                    .tag(activity)
                    .params(params)

                    .headers("token", RongCloudXML.getUserToken(activity,"token"))
                    .execute(new StringCallback() {

                        ProgressDialog dialog = CustomDialog.progressDialog(activity);

                        @Override
                        public void onBefore(BaseRequest request) {
                            super.onBefore(request);
                            if (!activity.isFinishing() && dialog != null && !dialog.isShowing() && !StringUtil.isStrEmpty(dialogMsg)) {
                                dialog.setMessage(dialogMsg);
                                dialog.show();
                            }
                        }
                        @Override
                        public String parseNetworkResponse(Response response) throws Exception {
                            return super.parseNetworkResponse(response);
                        }
                        @Override
                        public void onResponse(boolean isFromCache, String s, Request
                                request, @Nullable Response response) {
                            Log.e("1=====",s);
                            try {
                                String retCode = new JSONObject(s).getString("status");
                                String retMsg = new JSONObject(s).getString("msg");
                                if (Constant.RESPONSE_CODE_SUCCESS.equals(retCode)) {
                                    msg.what = success;
                                    msg.obj = new JSONObject(s).getString("data");
                                } else {//TODO 请求失败返回
                                    msg.what = failed;
                                    msg.obj = retMsg;
                                    if (null != retCode) msg.arg1 = Integer.valueOf(retCode);
                                }

                            } catch (JSONException e) {
                                e.printStackTrace();
                            }handler.sendMessage(msg);

                        }

                        @Override
                        public void onError(boolean isFromCache, Call call, @Nullable Response response, @Nullable Exception e) {
                            super.onError(isFromCache, call, response, e);
                            if (dialog != null && dialog.isShowing())
                                dialog.dismiss();
                            if (e instanceof SocketTimeoutException || e instanceof TimeoutException)
                                msg.obj = "访问超时，请稍后重试";
                            else
                                msg.obj = "网络繁忙，请稍后重试";
                            msg.what = failed;

                            handler.sendMessage(msg);
                        }

                        @Override
                        public void onAfter(boolean isFromCache, @Nullable String s, Call call, @Nullable Response response, @Nullable Exception e) {
                            super.onAfter(isFromCache, s, call, response, e);
                            if (dialog != null && dialog.isShowing())
                                dialog.dismiss();
                        }
                    });
        }
    }



    /**
     * put访问服务器
     *
     * @param activity
     * @param httpUrl
     * @param dialogMsg 加载框提示语 如果为空 则不显示加载框
     * @param params
     * @param handler
     * @param success
     * @param failed
     */
    public static void get(final Activity activity, String httpUrl, final String dialogMsg, HttpParams params, final Handler handler, final int success,
                           final int failed) {

        final Message msg = new Message();


        params.put("loginUserId",UserXML.getUserId(activity));
        params.put("subjectInfoId", RongCloudXML.getSubjectInfoId(activity));
        params.put("tenantCode", StringUtils.isEmpty(RongCloudXML.getTenantCode(activity))?"T000010":RongCloudXML.getTenantCode(activity));
        if (!Utils.isInternetAvailable(activity)) {
            msg.what = failed;
            msg.obj = "网络链接不可用，请检查网络";
            handler.sendMessage(msg);
        } else {
            OkHttpUtils.get(httpUrl)
                    .tag(activity)
                    .params(params)

                    .headers("token",RongCloudXML.getUserToken(activity,"token"))
                    .execute(new StringCallback() {

                        ProgressDialog dialog = CustomDialog.progressDialog(activity);

                        @Override
                        public void onBefore(BaseRequest request) {
                            super.onBefore(request);
                            if (!activity.isFinishing() && dialog != null && !dialog.isShowing() && !StringUtil.isStrEmpty(dialogMsg)) {
                                dialog.setMessage(dialogMsg);
                                dialog.show();
                            }
                        }
                        @Override
                        public String parseNetworkResponse(Response response) throws Exception {
                            return super.parseNetworkResponse(response);
                        }
                        @Override
                        public void onResponse(boolean isFromCache, String s, Request
                                request, @Nullable Response response) {
                            Log.e("1=====",s);
                            try {
                                String retCode = new JSONObject(s).getString("status");
                                String retMsg = new JSONObject(s).getString("msg");
                                if (Constant.RESPONSE_CODE_SUCCESS.equals(retCode)) {
                                    msg.what = success;
                                    msg.obj = new JSONObject(s).getString("data");
                                } else {//TODO 请求失败返回
                                    msg.what = failed;
                                    msg.obj = retMsg;
                                    if (null != retCode) msg.arg1 = Integer.valueOf(retCode);
                                }

                            } catch (JSONException e) {
                                e.printStackTrace();
                            }handler.sendMessage(msg);

                        }

                        @Override
                        public void onError(boolean isFromCache, Call call, @Nullable Response response, @Nullable Exception e) {
                            super.onError(isFromCache, call, response, e);
                            if (dialog != null && dialog.isShowing())
                                dialog.dismiss();
                            if (e instanceof SocketTimeoutException || e instanceof TimeoutException)
                                msg.obj = "访问超时，请稍后重试";
                            else
                                msg.obj = "网络繁忙，请稍后重试";
                            msg.what = failed;

                            handler.sendMessage(msg);
                        }

                        @Override
                        public void onAfter(boolean isFromCache, @Nullable String s, Call call, @Nullable Response response, @Nullable Exception e) {
                            super.onAfter(isFromCache, s, call, response, e);
                            if (dialog != null && dialog.isShowing())
                                dialog.dismiss();
                        }
                    });
        }
    }


    /**
     * put访问服务器
     *
     * @param activity
     * @param httpUrl
     * @param dialogMsg 加载框提示语 如果为空 则不显示加载框
     * @param params
     * @param handler
     * @param success
     * @param failed
     */
    public static void getA(final Activity activity, String httpUrl, final String dialogMsg, HttpParams params, final Handler handler, final int success,
                            final int failed) {

        final Message msg = new Message();


        LogUtils.e(params);
        params.put("loginUserId",UserXML.getUserId(activity));
        params.put("subjectInfoId", RongCloudXML.getSubjectInfoId(activity));
        params.put("tenantCode", StringUtils.isEmpty(RongCloudXML.getTenantCode(activity))?"T000010":RongCloudXML.getTenantCode(activity));
        if (!Utils.isInternetAvailable(activity)) {
            msg.what = failed;
            msg.obj = "网络链接不可用，请检查网络";
            handler.sendMessage(msg);
        } else {
            OkHttpUtils.get(httpUrl)
                    .tag(activity)
                    .params(params)

                    .headers("token",RongCloudXML.getUserToken(activity,"token"))
                    .execute(new StringCallback() {

                        ProgressDialog dialog = CustomDialog.progressDialog(activity);

                        @Override
                        public void onBefore(BaseRequest request) {
                            super.onBefore(request);
                            if (!activity.isFinishing() && dialog != null && !dialog.isShowing() && !StringUtil.isStrEmpty(dialogMsg)) {
                                dialog.setMessage(dialogMsg);
                                dialog.show();
                            }
                        }
                        @Override
                        public String parseNetworkResponse(Response response) throws Exception {
                            return super.parseNetworkResponse(response);
                        }
                        @Override
                        public void onResponse(boolean isFromCache, String s, Request
                                request, @Nullable Response response) {
                            Log.e("1=====",s);
                            try {
                                String retCode = new JSONObject(s).getString("status");
                                String retMsg = new JSONObject(s).getString("msg");
                                if (Constant.RESPONSE_CODE_SUCCESS.equals(retCode)) {
                                    msg.what = success;
                                    msg.obj = new JSONObject(s).getString("data");
                                } else {//TODO 请求失败返回
                                    msg.what = failed;
                                    msg.obj = retMsg;
                                    if (null != retCode) msg.arg1 = Integer.valueOf(retCode);
                                }

                            } catch (JSONException e) {
                                e.printStackTrace();
                            }handler.sendMessage(msg);

                        }

                        @Override
                        public void onError(boolean isFromCache, Call call, @Nullable Response response, @Nullable Exception e) {
                            super.onError(isFromCache, call, response, e);
                            if (dialog != null && dialog.isShowing())
                                dialog.dismiss();
                            if (e instanceof SocketTimeoutException || e instanceof TimeoutException)
                                msg.obj = "访问超时，请稍后重试";
                            else
                                msg.obj = "网络繁忙，请稍后重试";
                            msg.what = failed;

                            handler.sendMessage(msg);
                        }

                        @Override
                        public void onAfter(boolean isFromCache, @Nullable String s, Call call, @Nullable Response response, @Nullable Exception e) {
                            super.onAfter(isFromCache, s, call, response, e);
                            if (dialog != null && dialog.isShowing())
                                dialog.dismiss();
                        }
                    });
        }
    }



    /**
     * put访问服务器
     *
     * @param activity
     * @param httpUrl
     * @param dialogMsg 加载框提示语 如果为空 则不显示加载框
     * @param params
     * @param handler
     * @param success
     * @param failed
     */
    public static void post(final Activity activity, String httpUrl, final String dialogMsg, HttpParams params, final Handler handler, final int success,
                            final int failed) {

        params.put("tenantCode", StringUtils.isEmpty(RongCloudXML.getTenantCode(activity))?"T000010":RongCloudXML.getTenantCode(activity));
        params.put("subjectInfoId", RongCloudXML.getSubjectInfoId(activity));
        params.put("loginUserId",UserXML.getUserId(activity));

        final Message msg = new Message();
        LogUtils.e("1----------"+params);
        if (!Utils.isInternetAvailable(activity)) {
            msg.what = failed;
            msg.obj = "网络链接不可用，请检查网络";
            handler.sendMessage(msg);
        } else {
            OkHttpUtils.post(httpUrl)
                    .tag(activity).params(params)
                    .headers("token",RongCloudXML.getUserToken(activity,"token"))
                    .mediaType(MEDIA_TYPE_JSON)
                    .execute(new StringCallback() {
                        ProgressDialog dialog = CustomDialog.progressDialog(activity);
                        @Override
                        public void onBefore(BaseRequest request) {
                            super.onBefore(request);
                            if (!activity.isFinishing() && dialog != null && !dialog.isShowing() && !StringUtil.isStrEmpty(dialogMsg)) {
                                dialog.setMessage(dialogMsg);
                                dialog.show();
                            }

                        }

                        @Override
                        public String parseNetworkResponse(Response response) throws Exception {
                            return super.parseNetworkResponse(response);
                        }

                        @Override
                        public void onResponse(boolean isFromCache, String s, Request
                                request, @Nullable Response response) {

                            Log.d("1=====",s);

                            HeaderVO header = getHeaderBean(s, "header");
                            if (header != null) {
                                if ("0".equals(header
                                        .getRetCode())) {
                                    msg.what = success;
                                    msg.obj = s;
                                } else {//TODO 请求失败返回
                                    msg.what = failed;
                                    msg.obj = header.getRetMsg();
                                    if (null != header.getRetCode())
                                        msg.arg1 = Integer.valueOf(header.getRetCode());
                                }
                            } else {
                                ResultVO resultVO = getResultBean(s);

                                if (resultVO != null && resultVO.getStatus().equalsIgnoreCase("0")&& resultVO.getMsg().equalsIgnoreCase("操作成功")){
                                    msg.what = success;
                                    msg.obj = s;
                                }else {
                                    msg.what = failed;
                                    msg.obj = resultVO.getMsg();
                                }
                            }
                            handler.sendMessage(msg);

                        }

                        @Override
                        public void onError(boolean isFromCache, Call call, @Nullable Response response, @Nullable Exception e) {
                            super.onError(isFromCache, call, response, e);
                            if (dialog != null && dialog.isShowing())
                                dialog.dismiss();
                            if (e instanceof SocketTimeoutException || e instanceof TimeoutException)
                                msg.obj = "访问超时，请稍后重试";
                            else
                                msg.obj = "网络繁忙，请稍后重试";
                            msg.what = failed;

                            handler.sendMessage(msg);
                        }

                        @Override
                        public void onAfter(boolean isFromCache, @Nullable String s, Call call, @Nullable Response response, @Nullable Exception e) {
                            super.onAfter(isFromCache, s, call, response, e);
                            if (dialog != null && dialog.isShowing())
                                dialog.dismiss();
                        }
                    });
        }
    }







    /**
     * put访问服务器
     *
     * @param activity
     * @param httpUrl
     * @param dialogMsg 加载框提示语 如果为空 则不显示加载框
     * @param params
     * @param handler
     * @param success
     * @param failed
     */
    public static void postP(final Activity activity, String httpUrl, final String dialogMsg, HttpParams params, final Handler handler, final int success,
                             final int failed) {

        final Message msg = new Message();
        LogUtils.e("1----------"+params);
        if (!Utils.isInternetAvailable(activity)) {
            msg.what = failed;
            msg.obj = "网络链接不可用，请检查网络";
            handler.sendMessage(msg);
        } else {
            OkHttpUtils.post(httpUrl)
                    .tag(activity).params(params)
                    .headers("token",RongCloudXML.getUserToken(activity,"token"))
                    .execute(new StringCallback() {
                        ProgressDialog dialog = CustomDialog.progressDialog(activity);
                        @Override
                        public void onBefore(BaseRequest request) {
                            super.onBefore(request);
                            if (!activity.isFinishing() && dialog != null && !dialog.isShowing() && !StringUtil.isStrEmpty(dialogMsg)) {
                                dialog.setMessage(dialogMsg);
                                dialog.show();
                            }

                        }

                        @Override
                        public String parseNetworkResponse(Response response) throws Exception {
                            return super.parseNetworkResponse(response);
                        }

                        @Override
                        public void onResponse(boolean isFromCache, String s, Request
                                request, @Nullable Response response) {

                            Log.e("1=====",s);
                            try {
                                String retCode = new JSONObject(s).getString("status");
                                String retMsg = new JSONObject(s).getString("msg");
                                if (Constant.RESPONSE_CODE_SUCCESS.equals(retCode)) {
                                    msg.what = success;
                                    msg.obj = new JSONObject(s).getString("data");
                                } else {//TODO 请求失败返回
                                    msg.what = failed;
                                    msg.obj = retMsg;
                                    if (null != retCode) msg.arg1 = Integer.valueOf(retCode);
                                }

                            } catch (JSONException e) {
                                e.printStackTrace();
                            }handler.sendMessage(msg);

                        }

                        @Override
                        public void onError(boolean isFromCache, Call call, @Nullable Response response, @Nullable Exception e) {
                            super.onError(isFromCache, call, response, e);
                            if (dialog != null && dialog.isShowing())
                                dialog.dismiss();
                            if (e instanceof SocketTimeoutException || e instanceof TimeoutException)
                                msg.obj = "访问超时，请稍后重试";
                            else
                                msg.obj = "网络繁忙，请稍后重试";
                            msg.what = failed;

                            handler.sendMessage(msg);
                        }

                        @Override
                        public void onAfter(boolean isFromCache, @Nullable String s, Call call, @Nullable Response response, @Nullable Exception e) {
                            super.onAfter(isFromCache, s, call, response, e);
                            if (dialog != null && dialog.isShowing())
                                dialog.dismiss();
                        }
                    });
        }
    }



    /**
     * put访问服务器
     *
     * @param activity
     * @param httpUrl
     * @param dialogMsg 加载框提示语 如果为空 则不显示加载框
     * @param params
     * @param handler
     * @param success
     * @param failed
     */
    public static void postN(final Activity activity, String httpUrl, final String dialogMsg, JSONObject params, final Handler handler, final int success,
                             final int failed) {


        final Message msg = new Message();
        LogUtils.e(params);
        if (!Utils.isInternetAvailable(activity)) {
            msg.what = failed;
            msg.obj = "网络链接不可用，请检查网络";
            handler.sendMessage(msg);
        } else {
            OkHttpUtils.post(httpUrl)

                    .postJson(params.toString())

                    .headers("token",RongCloudXML.getUserToken(activity,"token"))

                    .execute(new StringCallback() {
                        ProgressDialog dialog = CustomDialog.progressDialog(activity);
                        @Override
                        public void onBefore(BaseRequest request) {
                            super.onBefore(request);
                            if (!activity.isFinishing() && dialog != null && !dialog.isShowing() && !StringUtil.isStrEmpty(dialogMsg)) {
                                dialog.setMessage(dialogMsg);
                                dialog.show();
                            }

                        }

                        @Override
                        public String parseNetworkResponse(Response response) throws Exception {
                            return super.parseNetworkResponse(response);
                        }

                        @Override
                        public void onResponse(boolean isFromCache, String s, Request
                                request, @Nullable Response response) {

                            Log.d("1=====",s);


                            try {
                                String retCode = new JSONObject(s).getString("status");
                                String retMsg = new JSONObject(s).getString("msg");
                                if (Constant.RESPONSE_CODE_SUCCESS.equals(retCode)) {
                                    msg.what = success;
                                    msg.obj = new JSONObject(s).getString("data");
                                } else {//TODO 请求失败返回
                                    msg.what = failed;
                                    msg.obj = retMsg;
                                    if (null != retCode) msg.arg1 = Integer.valueOf(retCode);
                                }

                            } catch (JSONException e) {
                                e.printStackTrace();
                            }
                            handler.sendMessage(msg);


                        }

                        @Override
                        public void onError(boolean isFromCache, Call call, @Nullable Response response, @Nullable Exception e) {
                            super.onError(isFromCache, call, response, e);
                            if (dialog != null && dialog.isShowing())
                                dialog.dismiss();
                            if (e instanceof SocketTimeoutException || e instanceof TimeoutException)
                                msg.obj = "访问超时，请稍后重试";
                            else
                                msg.obj = "网络繁忙，请稍后重试";
                            msg.what = failed;

                            handler.sendMessage(msg);
                        }

                        @Override
                        public void onAfter(boolean isFromCache, @Nullable String s, Call call, @Nullable Response response, @Nullable Exception e) {
                            super.onAfter(isFromCache, s, call, response, e);
                            if (dialog != null && dialog.isShowing())
                                dialog.dismiss();
                        }
                    });
        }
    }






    /**
     * put访问服务器
     *
     * @param activity
     * @param httpUrl
     * @param dialogMsg 加载框提示语 如果为空 则不显示加载框
     * @param params
     * @param handler
     * @param success
     * @param failed
     */
    public static void postDelete(final Activity activity, String httpUrl, final String dialogMsg, JSONObject params, final Handler handler, final int success,
                            final int failed) {
        HttpParams paramsL = new HttpParams();

        paramsL.put("loginUserId",UserXML.getUserId(activity));
        paramsL.put("subjectInfoId", RongCloudXML.getSubjectInfoId(activity));
        paramsL.put("tenantCode", StringUtils.isEmpty(RongCloudXML.getTenantCode(activity))?"T000010":RongCloudXML.getTenantCode(activity));


        httpUrl=httpUrl+"?";
        for (Map.Entry<String, List<String>> entry : paramsL.urlParamsMap.entrySet()) {
            httpUrl += entry.getKey() + "=" + entry.getValue().get(0) + "&";
        }

        final Message msg = new Message();
        LogUtils.e(params);
        if (!Utils.isInternetAvailable(activity)) {
            msg.what = failed;
            msg.obj = "网络链接不可用，请检查网络";
            handler.sendMessage(msg);
        } else {
            OkHttpUtils.post(httpUrl)

                    .postJson(params.toString())
                    .headers("token",RongCloudXML.getUserToken(activity,"token"))

                    .execute(new StringCallback() {
                        ProgressDialog dialog = CustomDialog.progressDialog(activity);
                        @Override
                        public void onBefore(BaseRequest request) {
                            super.onBefore(request);
                            if (!activity.isFinishing() && dialog != null && !dialog.isShowing() && !StringUtil.isStrEmpty(dialogMsg)) {
                                dialog.setMessage(dialogMsg);
                                dialog.show();
                            }

                        }

                        @Override
                        public String parseNetworkResponse(Response response) throws Exception {
                            return super.parseNetworkResponse(response);
                        }

                        @Override
                        public void onResponse(boolean isFromCache, String s, Request
                                request, @Nullable Response response) {

                            Log.d("1=====",s);


                            try {
                                String retCode = new JSONObject(s).getString("status");
                                String retMsg = new JSONObject(s).getString("msg");
                                if (Constant.RESPONSE_CODE_SUCCESS.equals(retCode)) {
                                    msg.what = success;
                                    msg.obj = new JSONObject(s).getString("data");
                                } else {//TODO 请求失败返回
                                    msg.what = failed;
                                    msg.obj = retMsg;
                                    if (null != retCode) msg.arg1 = Integer.valueOf(retCode);
                                }

                            } catch (JSONException e) {
                                e.printStackTrace();
                            }
                            handler.sendMessage(msg);


                        }

                        @Override
                        public void onError(boolean isFromCache, Call call, @Nullable Response response, @Nullable Exception e) {
                            super.onError(isFromCache, call, response, e);
                            if (dialog != null && dialog.isShowing())
                                dialog.dismiss();
                            if (e instanceof SocketTimeoutException || e instanceof TimeoutException)
                                msg.obj = "访问超时，请稍后重试";
                            else
                                msg.obj = "网络繁忙，请稍后重试";
                            msg.what = failed;

                            handler.sendMessage(msg);
                        }

                        @Override
                        public void onAfter(boolean isFromCache, @Nullable String s, Call call, @Nullable Response response, @Nullable Exception e) {
                            super.onAfter(isFromCache, s, call, response, e);
                            if (dialog != null && dialog.isShowing())
                                dialog.dismiss();
                        }
                    });
        }
    }





    /**
     * put访问服务器
     *
     * @param activity
     * @param httpUrl
     * @param dialogMsg 加载框提示语 如果为空 则不显示加载框
     * @param params
     * @param handler
     * @param success
     * @param failed
     */
    public static void post(final Activity activity, String httpUrl, final String dialogMsg, JSONObject params, final Handler handler, final int success,
                            final int failed) {

        HttpParams paramsL = new HttpParams();
        paramsL.put("loginUserId",UserXML.getUserId(activity));

        httpUrl=httpUrl+"?";
        for (Map.Entry<String, List<String>> entry : paramsL.urlParamsMap.entrySet()) {
            httpUrl += entry.getKey() + "=" + entry.getValue().get(0) + "&";
        }

        final Message msg = new Message();
        LogUtils.e(params);
        if (!Utils.isInternetAvailable(activity)) {
            msg.what = failed;
            msg.obj = "网络链接不可用，请检查网络";
            handler.sendMessage(msg);
        } else {
            OkHttpUtils.post(httpUrl)

                    .postJson(params.toString())
                    .headers("token",RongCloudXML.getUserToken(activity,"token"))

                    .execute(new StringCallback() {
                        ProgressDialog dialog = CustomDialog.progressDialog(activity);
                        @Override
                        public void onBefore(BaseRequest request) {
                            super.onBefore(request);
                            if (!activity.isFinishing() && dialog != null && !dialog.isShowing() && !StringUtil.isStrEmpty(dialogMsg)) {
                                dialog.setMessage(dialogMsg);
                                dialog.show();
                            }

                        }

                        @Override
                        public String parseNetworkResponse(Response response) throws Exception {
                            return super.parseNetworkResponse(response);
                        }

                        @Override
                        public void onResponse(boolean isFromCache, String s, Request
                                request, @Nullable Response response) {

                            Log.d("1=====",s);


                            try {
                                String retCode = new JSONObject(s).getString("status");
                                String retMsg = new JSONObject(s).getString("msg");
                                if (Constant.RESPONSE_CODE_SUCCESS.equals(retCode)) {
                                    msg.what = success;
                                    msg.obj = new JSONObject(s).getString("data");
                                } else {//TODO 请求失败返回
                                    msg.what = failed;
                                    msg.obj = retMsg;
                                    if (null != retCode) msg.arg1 = Integer.valueOf(retCode);
                                }

                            } catch (JSONException e) {
                                e.printStackTrace();
                            }
                            handler.sendMessage(msg);


                        }

                        @Override
                        public void onError(boolean isFromCache, Call call, @Nullable Response response, @Nullable Exception e) {
                            super.onError(isFromCache, call, response, e);
                            if (dialog != null && dialog.isShowing())
                                dialog.dismiss();
                            if (e instanceof SocketTimeoutException || e instanceof TimeoutException)
                                msg.obj = "访问超时，请稍后重试";
                            else
                                msg.obj = "网络繁忙，请稍后重试";
                            msg.what = failed;

                            handler.sendMessage(msg);
                        }

                        @Override
                        public void onAfter(boolean isFromCache, @Nullable String s, Call call, @Nullable Response response, @Nullable Exception e) {
                            super.onAfter(isFromCache, s, call, response, e);
                            if (dialog != null && dialog.isShowing())
                                dialog.dismiss();
                        }
                    });
        }
    }




    /**
     * 解析header
     *
     * @param jsonStr
     * @param headKey
     * @return
     */
    public static HeaderVO getHeaderBean(String jsonStr, String headKey) {

        try {
            String data;
            if (!StringUtil.isStrEmpty(headKey)) {
                data = new JSONObject(jsonStr).getString(headKey);
            } else {
                data = new JSONObject(jsonStr).toString();
            }
            HeaderVO headerVO = JSON.parseObject(data, HeaderVO.class);
            return headerVO;
        } catch (JSONException e) {
            return null;
        }
    }

    /**
     * 解析header
     *
     * @param jsonStr
     * @return
     */
    public static ResultVO getResultBean(String jsonStr) {
        try {
            ResultVO resultVO = JSON.parseObject(jsonStr, ResultVO.class);
            return resultVO;
        }catch (Exception e){
            return null;
        }

    }

}
